package com.xingkeduo.business.user.service;

import com.xingkeduo.dao.database.DatabaseAccessObject;
import com.xingkeduo.pojo.query.JobseekerWechatQO;
import com.xingkeduo.pojo.table.JobseekerWechatTO;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.util.tool.CommonUtil;
import com.xingkeduo.util.tool.IdentityUtil;
import com.xingkeduo.util.tool.IdentityUtil.Worker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:
 * @Author:gaozifeng
 * @Date 2019/2/18 18:29
 **/
@Service
public class JobseekerWechatService {
    private static final String NAMESPACE = CommonUtil.formatNamespace(JobseekerWechatTO.class);
    @Resource(name = "mysqlUserMasterDao")
    private DatabaseAccessObject<JobseekerWechatTO, JobseekerWechatQO> dao;


    /**
     * 消费者分页列表
     *
     * @return : List<ConsumerTO>
     * @author : gaozifeng
     * @date : 2019年2月26日20:29:33
     */
    public List<JobseekerWechatTO> selectConsumerList(JobseekerWechatQO jobseekerWechatQO) throws Throwable {
        return dao.select(NAMESPACE, jobseekerWechatQO);
    }

    /**
     * 消费者分页列表
     *
     * @return : List<ConsumerTO>
     * @author : gaozifeng
     * @date : 2019年2月26日20:29:33
     */
    public List<Long> selectListForid(JobseekerWechatQO jobseekerWechatQO) throws Throwable {
        return dao.selectIndex(NAMESPACE, jobseekerWechatQO);
    }

    /**
     * 新增消费者
     *
     * @param jobseekerWechatTO:
     * @return : int
     * @author : gaozifeng
     * @date : 2019/2/19 11:27
     */
    @Transactional
    public void insert(JobseekerWechatTO jobseekerWechatTO) throws Throwable {
        jobseekerWechatTO.initId(IdentityUtil.nextId(Worker.USER));
        jobseekerWechatTO.setUpdatedBy(jobseekerWechatTO.getId());
        jobseekerWechatTO.setCreatedBy(jobseekerWechatTO.getId());
        if (dao.insert(NAMESPACE, jobseekerWechatTO) < 1) {
            List<JobseekerWechatTO> select = dao.select(NAMESPACE, new JobseekerWechatQO().setLogin(jobseekerWechatTO.getLogin()));
            if (null == select || select.size() <= 0) {
                throw new DataException(DataException.Type.FAIL, "新增消费者信息");
            } else {
                throw new DataException(DataException.Type.NOTONLY, "新增消费者不唯一");
            }
        }
    }

    /**
     * 查询单数据
     *
     * @param id:
     * @return : com.xingkeduo.pojo.table.ConsumerTO
     * @author : gaozifeng
     * @date : 2019/2/27 18:18
     */
    public JobseekerWechatTO selectByPrimary(long id) throws Throwable {
        return dao.select(NAMESPACE, id);
    }

}
